package UIFree.controls.FloatingArea
{
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class uiff_area extends uif_ecomponent {
		private var _refreshState: Boolean;
		public function uiff_area(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
		}
		
		override public function addChild(child: DisplayObject): DisplayObject {
			super.addChild(child);
			if (child is uif_component) {
				refreshSizeFromChilds();
				addEvent(RESIZE, doChildUpdate, child);
				addEvent(POSITION, doChildUpdate, child);
			}
			return child;
		}
		
		protected function doChildUpdate(e: Event): void {
			if (!_refreshState) refreshSizeFromChilds();
		}
		
		protected function refreshSizeFromChilds(): void {
			_refreshState = true;
			var rect: Rectangle = childBounds;
			if (!rect.equals(getInner())) {
				var space: Number = getSpace();
				rect.inflate(space, space);
				forEach(function(item: uif_component): void {
					item.position = item.position.subtract(rect.topLeft);
				}, uif_component);
				
				rect.x += position.x;
				rect.y += position.y;
				setRect(rect);
			}
			_refreshState = false;
		}
		
		public function get nextXPos(): Number {
			return childBounds.right;
		}
		
		public function get nextYPos(): Number {
			return childBounds.bottom;
		}
	}
}